﻿using azure_functions;
using azure_functions.Domain;
using Microsoft.Azure.Functions.Extensions.DependencyInjection;
using Microsoft.Azure.WebJobs.Host.Bindings;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;

[assembly: FunctionsStartup(typeof(Startup))]

namespace azure_functions;

public class Startup : FunctionsStartup
{
    private IConfiguration Configuration { get; set; }

    public override void Configure(IFunctionsHostBuilder builder)
    {
        InitializeConfiguration(builder);

        builder.Services.AddSingleton(Configuration.Get<SuperComputerSettings>());

        builder.Services.AddTransient<IHitchhikerGuideToTheGalaxy, HitchhikerGuideToTheGalaxy>();
        builder.Services.AddTransient<ISuperComputer, SuperComputer>();
    }

    private void InitializeConfiguration(IFunctionsHostBuilder builder)
    {
        var executionContextOptions = builder
            .Services
            .BuildServiceProvider()
            .GetService<IOptions<ExecutionContextOptions>>()
            .Value;

        Configuration = new ConfigurationBuilder()
            .SetBasePath(executionContextOptions.AppDirectory)
            .AddJsonFile("appsettings.json")
            .AddJsonFile("appsettings.Development.json", optional: true)
            .AddEnvironmentVariables()
            .Build();
    }
}